package ${filePackageName};

import ${utilsPackage}.DESUtil;

/**
 * Token Redis处理
 * @author ${projectAuthor}
 * @version 1.0
 * @date ${nowDate}
 */
public class TokenRedis extends Token {

    public static final String REDIS_KEY_TOKEN = "_token";

    @Override
    public void setData(TokenData tokenData) {
        //生成规则
        String accessKey = tokenData.getCustom().getAccessKey();
        long millis =System.currentTimeMillis();
        String key = millis+accessKey+tokenData.getTokenValue();
        String token = DESUtil.getInstance(accessKey).encrypt(key).replaceAll("[\\s*\t\n\r]", "");
        this.setToken(token);
        this.setAccessKey(accessKey);
        this.setStartTime(millis);
        this.setEndTime(millis+tokenData.getExpireTime());
        this.setExpireTime(tokenData.getExpireTime());
        this.setTokenValue(tokenData.getTokenValue());
    }


    /**
     * 获取data
     *
     * @return
     */
    @Override
    public String getData(boolean valid) {
        if(validate()){
            this.continueLife();
            return this.getTokenValue();
        }
        return null;
    }

    /**
     * 延长有效期
     */
    @Override
    public void continueLife() {
        this.setStartTime(System.currentTimeMillis());
        this.setEndTime(System.currentTimeMillis()+this.getExpireTime());
    }

    /**
     * 校验token
     *
     * @return
     */
    @Override
    public boolean validate() {
        if(this.getEndTime() > System.currentTimeMillis()){
            return true;
        }
        return false;
    }
}